<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\CommController;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\Http\Model\User;
use Illuminate\Support\Facades\Crypt;

class IndexController extends CommController
{
    //
    public function index(){
        return view('admin.index');
    }
    public function info(){
        return view('admin.info');
    }
    public function logout(){
        session()->flush();
        return redirect('admin/login');
    }
    public function pass(){
        if($input=Input::all()){
            $rules=[
                'password'=>"required|between:6,20|confirmed",
            ];
            $message=[
                'password.required'=>"密码不能为空！",
                'password.between'=>"密码长度要求6-20位！",
                'password.confirmed'=>"两次密码不一致！！"
            ];
            $validator=Validator::make($input,$rules,$message);
            if($validator->passes()){
                $user=User::where('username',session('user')['username'])->first();
                if(Crypt::decrypt($user->password)==$input['password_o']){
                    $user->password=Crypt::encrypt($input['password']);
                    $user->save();
                    return back()->with('msg',"密码修改成功！");
                }else {
                    return back()->with('msg','原密码错误！');
                }
            }else {
                return back()->withErrors($validator);
            }
        }else {
            return view('admin.pass');
        }
    }
}
